import { requestClient } from './request';

/**
 * 获取知识库列表
 */
export function getKnowledgeBaseList() {
  return requestClient.get('/api/knowledge-base');
}

/**
 * 获取知识库详情
 * @param id 知识库ID
 */
export function getKnowledgeBaseDetail(id: string) {
  return requestClient.get(`/api/knowledge-base/${id}`);
}

/**
 * 创建知识库
 * @param data 知识库数据
 */
export function createKnowledgeBase(data: any) {
  return requestClient.post('/api/knowledge-base', data);
}

/**
 * 更新知识库
 * @param id 知识库ID
 * @param data 知识库数据
 */
export function updateKnowledgeBase(id: string, data: any) {
  return requestClient.put(`/api/knowledge-base/${id}`, data);
}

/**
 * 删除知识库
 * @param id 知识库ID
 */
export function deleteKnowledgeBase(id: string) {
  return requestClient.delete(`/api/knowledge-base/${id}`);
}

/**
 * 上传文件到知识库
 * @param baseId 知识库ID
 * @param file 文件
 */
export function uploadFileToKnowledgeBase(baseId: string, file: File) {
  const formData = new FormData();
  formData.append('file', file);
  // formData.append('baseId', baseId);
  return requestClient.post(`/api/file/upload`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

// /**
//  * 在知识库中搜索
//  * @param baseId 知识库ID
//  * @param text 搜索文本
//  * @param topK 返回结果数量
//  */
// export function searchInKnowledgeBase(
//   baseId: string,
//   text: string,
//   topK: number = 5,
// ) {
//   return requestClient.get(`/api/vector/knowledge-base/${baseId}/search`, {
//     params: {
//       text,
//       topK,
//     },
//   });
// }

/**
 * 汽车知识问答
 * @param question 问题
 * @param knowledgeBaseId 知识库ID
 */
export function queryCarKnowledge(question: string, knowledgeBaseId?: string) {
  return requestClient.post('/api/agent/car/query', null, {
    params: {
      question,
      knowledgeBaseId,
    },
  });
}

/**
 * 汽车知识简答
 * @param question 问题
 * @param knowledgeBaseId 知识库ID
 */
export function getCarKnowledgeAnswer(
  question: string,
  knowledgeBaseId?: string,
) {
  return requestClient.post(
    '/api/agent/car/query',
    {
      question,
      knowledgeBaseId,
    },
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    },
  );
}

/**
 * 知识库搜索
 * @param text 搜索文本
 * @param topK 返回结果数量
 * @param baseId 知识库ID
 */
export function searchKnowledgeBase(
  text: string,
  topK: number = 5,
  baseId: string,
) {
  return requestClient.get('/api/vector/demo/search', {
    params: {
      text,
      topK,
      baseId,
    },
  });
}
